package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.db.HomeCollectEntity;
import com.lanou.connotationjoke.db.HomeCollectEntityDao;
import com.lanou.connotationjoke.entity.DataDetialEty;
import com.lanou.connotationjoke.tool.DaoSingleton;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.ShareSingleton;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/25.
 */
public class HomeRecommendationListVIewAdapter extends BaseAdapter{
    private Context context;
    private List<DataDetialEty> datas;
    private HomeCollectEntityDao homeCollectEntityDao
            = DaoSingleton.getInstance().getHomeCollectEntityDao();
    public HomeRecommendationListVIewAdapter(Context context, List<DataDetialEty> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        DataDetialEty dataDetialEty = datas.get(position);
        if (dataDetialEty.getGroup().getMp4_url()!=null){
            return 2;
        }else if (dataDetialEty.getGroup().getLarge_image()!=null){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (datas.get(position).getGroup() != null) {
            int type = getItemViewType(position);
            JokeViewHolder jokeViewHolder = null;
            PictureViewHolder pictureViewHolder = null;
            VideoViewHolder videoViewHolder = null;
            if (convertView == null) {
                switch (type) {
                    case 0:
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke, null);
                        jokeViewHolder = new JokeViewHolder(convertView);
                        convertView.setTag(jokeViewHolder);
                        break;
                    case 1:
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_picture, null);
                        pictureViewHolder = new PictureViewHolder(convertView);
                        convertView.setTag(pictureViewHolder);
                        break;
                    case 2:
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_video_test, null);
                        videoViewHolder = new VideoViewHolder(convertView);
                        convertView.setTag(videoViewHolder);
                        break;
                }
            }else {
                switch (type) {
                    case 0:
                    jokeViewHolder = (JokeViewHolder) convertView.getTag();
                        break;
                    case 1:
                    pictureViewHolder = (PictureViewHolder) convertView.getTag();
                        break;
                    case 2:
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_video, null);
                        videoViewHolder = new VideoViewHolder(convertView);
                        break;
                }
            }
            switch (type) {
                case 0:
                    jokeViewHolder.diggTv.setText("" + datas.get(position).getGroup().getDigg_count());
                    jokeViewHolder.contentTv.setText(datas.get(position).getGroup().getContent());
                    jokeViewHolder.userNameTv.setText(datas.get(position).getGroup().getUser().getName());
                    jokeViewHolder.categoryName_Tv.setText(datas.get(position).getGroup().getCategory_name());
                    jokeViewHolder.buryTv.setText("" + datas.get(position).getGroup().getBury_count());
                    jokeViewHolder.hotCommentTv.setText("" + datas.get(position).getGroup().getComment_count());
                    jokeViewHolder.sendTv.setText("" + datas.get(position).getGroup().getShare_count());
                    jokeViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                    jokeViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                    jokeViewHolder.userPictureIv.setImageUrl(datas.get(position).getGroup().getUser().getAvatar_url()
                            , VolleySingleton.getImageLoader());
                    jokeViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()) {
                                String url = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                                WebViewActivity.goToWebViewActivity(context, url);
                            } else {
                                Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    jokeViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()){
                                String title = "超级搞笑";
                                String titleurl = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                                String content = datas.get(position).getGroup().getContent();
                                String imageurl = datas.get(position).getGroup().getUser().getAvatar_url();
                                ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                            }else {
                                Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    jokeViewHolder.collectTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            insertDataToHomeCollectDao(datas.get(position));
                            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                case 1:
                    if (MyAppllication.hasNetwork()) {
                        Glide.with(context).load(datas.get(position)
                                .getGroup()
                                .getLarge_image()
                                .getUri())
                                .into(pictureViewHolder.largeIv);
                    }
                    pictureViewHolder.diggTv.setText("" + datas.get(position).getGroup().getDigg_count());
                    pictureViewHolder.contentTv.setText(datas.get(position).getGroup().getContent());
                    pictureViewHolder.userNameTv.setText(datas.get(position).getGroup().getUser().getName());
                    pictureViewHolder.categoryName_Tv.setText(datas.get(position).getGroup().getCategory_name());
                    pictureViewHolder.buryTv.setText("" + datas.get(position).getGroup().getBury_count());
                    pictureViewHolder.hotCommentTv.setText("" + datas.get(position).getGroup().getComment_count());
                    pictureViewHolder.sendTv.setText("" + datas.get(position).getGroup().getShare_count());
                    pictureViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                    pictureViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                    pictureViewHolder.userPictureIv.setImageUrl(datas.get(position).getGroup().getUser().getAvatar_url()
                            , VolleySingleton.getImageLoader());
                    pictureViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()) {
                                String url = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                                WebViewActivity.goToWebViewActivity(context, url);
                            } else {
                                Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    pictureViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()){
                                String title = "超级搞笑";
                                String titleurl = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                                String content = datas.get(position).getGroup().getContent();
                                String imageurl = datas.get(position).getGroup().getUser().getAvatar_url();
                                ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                            }else {
                                Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    pictureViewHolder.collectTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            insertDataToHomeCollectDao(datas.get(position));
                            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                    });


                    break;
                case 2:
                    videoViewHolder.playCountTv.setText(""+datas.get(position).getGroup().getPlay_count());
                    videoViewHolder.diggTv.setText("" + datas.get(position).getGroup().getDigg_count());
                    videoViewHolder.contentTv.setText(datas.get(position).getGroup().getContent());
                    videoViewHolder.userNameTv.setText(datas.get(position).getGroup().getUser().getName());
                    videoViewHolder.categoryName_Tv.setText(datas.get(position).getGroup().getCategory_name());
                    videoViewHolder.buryTv.setText("" + datas.get(position).getGroup().getBury_count());
                    videoViewHolder.hotCommentTv.setText("" + datas.get(position).getGroup().getComment_count());
                    videoViewHolder.sendTv.setText("" + datas.get(position).getGroup().getShare_count());
                    videoViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                    videoViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                    videoViewHolder.userPictureIv.setImageUrl(datas.get(position).getGroup().getUser().getAvatar_url()
                            , VolleySingleton.getImageLoader());
                    videoViewHolder.coverImageIv.setDefaultImageResId(R.mipmap.ic_launcher);
                    videoViewHolder.coverImageIv.setErrorImageResId(R.mipmap.ic_launcher);
                    videoViewHolder.coverImageIv.setImageUrl(datas.get(position).getGroup().getCover_image_uri(),
                            VolleySingleton.getImageLoader());
                    final VideoViewHolder finalVideoViewHolder = videoViewHolder;
                    videoViewHolder.coverImageIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()){
                                finalVideoViewHolder.playVideo.setVisibility(View.GONE);
                                finalVideoViewHolder.progressBar.setVisibility(View.VISIBLE);
                                finalVideoViewHolder.videoView.setVideoURI(Uri.parse(datas.get(position).getGroup().getMp4_url()));
                                finalVideoViewHolder.videoView.setMediaController(new MediaController(context));
                                finalVideoViewHolder.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mp) {
                                        finalVideoViewHolder.frameLayout.setVisibility(View.GONE);
                                        finalVideoViewHolder.videoView.requestFocus();
                                        finalVideoViewHolder.videoView.start();
                                    }
                                });
                                finalVideoViewHolder.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                                    @Override
                                    public boolean onError(MediaPlayer mp, int what, int extra) {
                                        Toast.makeText(context, "播放失败,网速不好", Toast.LENGTH_SHORT).show();
                                        return true;
                                    }
                                });
                                finalVideoViewHolder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mp) {
                                        Toast.makeText(context, "视频播放完毕", Toast.LENGTH_SHORT).show();
                                    }
                                });

                            }else {
                                Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    videoViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()){
                                final String url = "http://neihanshequ.com/p"+datas.get(position).getGroup().getShare_url().substring(37);
                                WebViewActivity.goToWebViewActivity(context, url);
                            }else {
                                Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });


                    videoViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (MyAppllication.hasNetwork()){
                                String title = "超级搞笑";
                                String titleurl = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                                String content = datas.get(position).getGroup().getContent();
                                String imageurl = datas.get(position).getGroup().getUser().getAvatar_url();
                                ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                            }else {
                                Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    videoViewHolder.collectTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            insertDataToHomeCollectDao(datas.get(position));
                            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                    });

                    break;
            }
        }

        return convertView;
    }
    public void refreshData(List<DataDetialEty> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }



    private void insertDataToHomeCollectDao(DataDetialEty dataDetialEty){
        String avatar_url = dataDetialEty.getGroup().getUser().getAvatar_url();
        String name = dataDetialEty.getGroup().getUser().getName();
        String content = dataDetialEty.getGroup().getContent();
        String category_name = dataDetialEty.getGroup().getCategory_name();
        int digg_count = dataDetialEty.getGroup().getDigg_count();
        int bury_count = dataDetialEty.getGroup().getBury_count();
        int comment_count = dataDetialEty.getGroup().getComment_count();
        int share_count = dataDetialEty.getGroup().getShare_count();
        String share_url = dataDetialEty.getGroup().getShare_url();
        String uri = null;
        if (null != dataDetialEty.getGroup().getLarge_image()){
            uri = dataDetialEty.getGroup().getLarge_image().getUri();
        }

        String coverImageUri = dataDetialEty.getGroup().getCover_image_uri();
        String mp4Url = dataDetialEty.getGroup().getMp4_url();
        int playCount = dataDetialEty.getGroup().getPlay_count();


        boolean differernt = true;
        List<HomeCollectEntity> homeCollectEntities = homeCollectEntityDao.queryBuilder().list();
        if (homeCollectEntities != null){
            for (HomeCollectEntity homeCollectEntity : homeCollectEntities){
                if (homeCollectEntity.getShare_url() == share_url){
                    differernt = false;
                }
            }
        }
        if (differernt){
            homeCollectEntityDao.insert(new HomeCollectEntity(homeCollectEntityDao.count(),avatar_url,name,content
                    ,category_name,uri,coverImageUri,mp4Url,playCount,digg_count,bury_count
                    ,comment_count,share_count,share_url));
        }
    }


    class JokeViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        ImageView shareIv;
        TextView collectTv;
        public JokeViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_joke);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }


    class PictureViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        ImageView largeIv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        ImageView shareIv;
        TextView collectTv;
        public PictureViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            largeIv = (ImageView) itemView.findViewById(R.id.iv_large_url);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_picture);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }



    class VideoViewHolder {
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        NetworkImageView coverImageIv;
        TextView playCountTv;
        VideoView videoView;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        View frameLayout;
        ImageView shareIv;
        ImageView playVideo;
        ProgressBar progressBar;
        TextView collectTv;
        public VideoViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            coverImageIv = (NetworkImageView) itemView.findViewById(R.id.iv_cover_image);
            playCountTv = (TextView) itemView.findViewById(R.id.tv_play_count);
            videoView = (VideoView) itemView.findViewById(R.id.video_view);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            frameLayout = itemView.findViewById(R.id.framelayout);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_video);
            playVideo = (ImageView) itemView.findViewById(R.id.iv_video_play);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar_home_video);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }



}
